﻿//TODO: Deshadow: Test

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Tries to remove all shadows.
    /// </summary>
    public class Deshadow : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Deshadow"/> class.
        /// </summary>
        public Deshadow()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Deshadow"/> class.
        /// </summary>
        /// <param name="_deshadow">The _deshadow.</param>
        internal Deshadow(Deshadow _deshadow)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //Important to do: 
            //Get background and foreground
            UnsafeBitmap coloredBackground = _bitmap;
            UnsafeBitmap coloredForeground = _bitmap;
            float back_r, back_g, back_b, back_sum;
            float fore_r, fore_g, fore_b, fore_sum;
            float product_r, product_g, product_b;
            float product_back_r, product_back_g, product_back_b;
            float BD = 0.0f;

            float CD_r, CD_g, CD_b, CD_squared = 7;

            for (int i = 0; i < coloredBackground.Width; i++)
            {
                for (int j = 0; j < coloredBackground.Height; j++)
                {
                    back_r = (float)coloredBackground[i, j, 0];
                    back_g = (float)coloredBackground[i, j, 1];
                    back_b = (float)coloredBackground[i, j, 2];
                    back_sum = back_r + back_g + back_b;
                    fore_r = (float)coloredForeground[i, j, 0];
                    fore_g = (float)coloredForeground[i, j, 1];
                    fore_b = (float)coloredForeground[i, j, 2];
                    fore_sum = fore_r + fore_g + fore_b;

                    product_r = fore_r * back_r;
                    product_g = fore_g * back_g;
                    product_b = fore_b * back_b;

                    product_back_r = back_r * back_r;
                    product_back_g = back_g * back_g;
                    product_back_b = back_b * back_b;

                    BD = (product_r + product_g + product_b) / (product_back_r + product_back_g + product_back_b);

                    CD_r = fore_r - BD * back_r;
                    CD_g = fore_g - BD * back_g;
                    CD_b = fore_b - BD * back_b;

                     /*Background*//*Foreground*/
                    _bitmap[i, j, PIXEL_MODE.GRAY_VALUE] = (CD_squared < 120.0f && BD < 1.0f && BD > 0.5f) ? 0: 255;
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Deshadow(this);
        }
    }
}
